package com.zheng.common.util;

import com.zheng.common.domain.Location;

public class LocationUtil {

    /** 地球半径 */
    private static final double EARTH_RADIUS = 6371000;
    /** 范围距离 */
    private double distance;
    /** 左上角 */
    private Location left_top = null;
    /** 右上角 */
    private Location right_top = null;
    /** 左下角 */
    private Location left_bottom = null;
    /** 右下角 */
    private Location right_bottom = null;

    private LocationUtil(double distance) {
        this.distance = distance;
    }

    private void getRectangle4Point(double lat, double lng) {

        double dlng = 2 * Math.asin(Math.sin(distance / (2 * EARTH_RADIUS))
                / Math.cos(lat));
        dlng = Math.toDegrees(dlng);

        double dlat = distance / EARTH_RADIUS;
        dlat = Math.toDegrees(dlat); // # 弧度转换成角度

        left_top = new Location(lat + dlat, lng - dlng);
        right_top = new Location(lat + dlat, lng + dlng);
        left_bottom = new Location(lat - dlat, lng - dlng);
        right_bottom = new Location(lat - dlat, lng + dlng);

    }

    public static double hav(double theta) {
        double s = Math.sin(theta / 2);
        return s * s;
    }

    public static double getDistance(double lat0, double lng0, double lat1,
                                     double lng1) {
        lat0 = Math.toRadians(lat0);
        lat1 = Math.toRadians(lat1);
        lng0 = Math.toRadians(lng0);
        lng1 = Math.toRadians(lng1);

        double dlng = Math.abs(lng0 - lng1);
        double dlat = Math.abs(lat0 - lat1);
        double h = hav(dlat) + Math.cos(lat0) * Math.cos(lat1) * hav(dlng);
        double distance = 2 * EARTH_RADIUS * Math.asin(Math.sqrt(h));

        return distance;
    }

    public static Location[] getRectangle4Point(double lat, double lng,
                                                double distance) {
        LocationUtil llu = new LocationUtil(distance);
        llu.getRectangle4Point(lat, lng);
        Location[] locations = new Location[4];
        locations[0] = llu.left_top;
        locations[1] = llu.right_top;
        locations[2] = llu.left_bottom;
        locations[3] = llu.right_bottom;
        return locations;
    }

    public static Double[] getLatAndLngRange(double lat,double lng,double distance){
        double range = 180 / Math.PI * distance / 6372797;//里面的 1 就代表搜索 1m 之内，单位m
        double lngR = range / Math.cos(lat * Math.PI / 180);
        double maxLat = lat + range;//最大纬度
        double minLat = lat - range;//最小纬度
        double maxLng = lng + lngR;//最大经度
        double minLng = lng - lngR;//最小经度
        return new Double[]{minLat,maxLat,minLng,maxLng};
    }

    public static void main(String[] args) {
        double lat = 30.500;
        double lng = 120.500;
        double distance = 10000d;
        Location[] locations = LocationUtil.getRectangle4Point(lat,
                lng, distance);

        System.out.println(locations[2].getLatitude()+"  "+locations[0].getLatitude()+"  "
        +"  "+locations[0].getLongitude()+"  "+locations[1].getLongitude());

        Double[] latlngs = getLatAndLngRange(lat,lng,distance);
        System.out.println(latlngs[0]+"  "+latlngs[1]+"  "
                +"  "+latlngs[2]+"  "+latlngs[3]);

//        String sql = "SELECT * FROM umbrella_hardware_machine WHERE latitude > "
//                + locations[2].getLatitude() + " AND latitude < "
//                + locations[0].getLatitude() + " AND longitude > "
//                + locations[0].getLongitude() + " AND longitude < "
//                + locations[1].getLongitude();
//        System.out.println(sql);
//
//        double lat1 = 30.495503391970406;
//        double lng1 = 120.49261708577215;
//        double d = LocationUtil.getDistance(lat, lng, lat1, lng1);
//        System.out.println(d);
    }
}